/**
 * User.java
 * Represents a user of the application.
 * 
 * @author Ryan Senkbeil
 */

package com.msoe.core.users;

import com.google.gwt.user.client.rpc.IsSerializable;

public class User implements IsSerializable
{
	private AccountType accountType;
    private String fName;
    private String lName;
    private Password password;
    private int userId;
    private String userName;
    private String email;

    public User()
    {
        this("");
    }
    
    public User(String uname)
    {
    	this(-1, "", "", new Password(), uname, AccountType.Standard, "");
    }
    
    public User(int uid, String firstName, String lastName, Password password, String username, AccountType type, String email)
    {
    	this.userId = uid;
    	this.fName = firstName;
    	this.lName = lastName;
    	this.password = password;
    	this.userName = username;
    	this.accountType = type;
    	this.email = email;
    }
    
    /**
     * Authenticates a user against the password typed in.
     * @param password
     * @return A User containing all information, otherwise null.
     * @throws Exception
     */
    public User authenticate(String password) throws Exception
    {
    	SaltedHash hash = SaltedHash.create(this.password.getSalt(), this.password.getHash());
    	
    	if(hash.verify(password)) return this;
    	else return null;
    }

    @Override
    public String toString()
    {
        return this.fName + " " + this.lName;
    }

    public AccountType getAccountType()
    {
    	return this.accountType;
    }
    
    public void setAccountType(AccountType type)
    {
    	this.accountType = type;
    }

    public String getFirstName()
    {
    	return this.fName;
    }
    
    public void setFirstName(String name)
    {
    	this.fName = name;
    }
    
    public String getLastName()
    {
    	return this.lName;
    }
    
    public void setLastName(String name)
    {
    	this.lName = name;
    }
    
    public Password getPassword()
    {
    	return this.password;
    }
    
    public void setPassword(Password password)
    {
    	this.password = password;
    }
    
    public int getUserID()
    {
    	return this.userId;
    }
    
    public void setUserID(int uid)
    {
    	this.userId = uid;
    }

    public String getUsername()
    {
        return this.userName;
    }
    
    public void setUsername(String name)
    {
    	this.userName = name;
    }
    
    public String getEmail()
    {
    	return this.email;
    }
    
    public void setEmail(String email)
    {
    	this.email = email;
    }
}
